<?php


namespace App\Tools;


use Firebase\JWT\JWT;
use Firebase\JWT\Key;

class JwtTools
{
    public static function gainToken($id){

        $key = md5('community');
        $time = time();
        $payload = [
            'iss' => config('jwt.iss'),
            'aud' => config('jwt.aud'),
            'iat' => $time,
            'nbf' => $time,
            'exp' =>$time +7200,
            'data'=>[
                'uid'=>$id
            ]

        ];

        /**
         * IMPORTANT:
         * You must specify supported algorithms for your application. See
         * https://tools.ietf.org/html/draft-ietf-jose-json-web-algorithms-40
         * for a list of spec-compliant algorithms.
         */
        $jwt = JWT::encode($payload, $key, 'HS256');

        return $jwt;
    }


    public static function transitionId($token){

        $key = md5('community');

        $decoded = JWT::decode($token, new Key($key, 'HS256'));
        $decoded_array = (array) $decoded;
        $id = $decoded_array['data']->uid;
        return $id;
    }
}
